var archiver = require('archiver');
var concatStream = require('concat-stream');
var path = require('path');

module.exports = function(options, modified, total, callback) {
  var list = options.modified ? modified : total;

  if (!list.length) {
    return false;
  }
  
  /*! http://mths.be/startswith v0.2.0 by @mathias */
	if (!String.prototype.startsWith) {
	  (function() {
		'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
		var defineProperty = (function() {
		  // IE 8 only supports `Object.defineProperty` on DOM elements
		  try {
			var object = {};
			var $defineProperty = Object.defineProperty;
			var result = $defineProperty(object, object, object) && $defineProperty;
		  } catch(error) {}
		  return result;
		}());
		var toString = {}.toString;
		var startsWith = function(search) {
		  if (this == null) {
			throw TypeError();
		  }
		  var string = String(this);
		  if (search && toString.call(search) == '[object RegExp]') {
			throw TypeError();
		  }
		  var stringLength = string.length;
		  var searchString = String(search);
		  var searchLength = searchString.length;
		  var position = arguments.length > 1 ? arguments[1] : undefined;
		  // `ToInteger`
		  var pos = position ? Number(position) : 0;
		  if (pos != pos) { // better `isNaN`
			pos = 0;
		  }
		  var start = Math.min(Math.max(pos, 0), stringLength);
		  // Avoid the `indexOf` call if no match is possible
		  if (searchLength + start > stringLength) {
			return false;
		  }
		  var index = -1;
		  while (++index < searchLength) {
			if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
			  return false;
			}
		  }
		  return true;
		};
		if (defineProperty) {
		  defineProperty(String.prototype, 'startsWith', {
			'value': startsWith,
			'configurable': true,
			'writable': true
		  });
		} else {
		  String.prototype.startsWith = startsWith;
		}
	  }());
	}

  var root = fis.project.getProjectPath();
  //console.log('');
  //console.log('=======================================');
  //console.log('root:' + root);
  var zipfile = archiver.create('zip');

  zipfile.pipe(concatStream(function(data) {
    var file = fis.file(root, options.filename || 'all.zip');
    file.setContent(data);

    if (!options.keep) {
      modified.splice(0, modified.length);
      total.splice(0, total.length);
    }

    modified.push(file);
    total.push(file);
    callback();
  }));

  list.forEach(function(file) {
	var pathSubstring = 1;
	if(options.startWithPath != ''){
		pathSubstring = options.startWithPath.length;
	}
    var filepath = file.getHashRelease().substring(pathSubstring);
	var filename = path.basename(filepath);
	
	// 过滤以 __ 开头的文件
	if(!filename.startsWith('__')){
		console.log('');
		console.log('=======================================');
		console.log('filepath:' + filepath);
		console.log('filename:' + filename);
		
		zipfile.append(file.getContent(), {
		  name: filepath
		});
	}
	
  });

  zipfile.finalize();
};

module.exports.options = {
  // 是否保留原始文件。
  keep: false,

  // 是否只打包修改过的。
  modified: false,
  
  startWithPath: '',

  // zip 文件名
  filename: 'all.zip'
};
